import Vue from 'vue'
import VueRouter from 'vue-router'
import { getItem } from '@/utils/auth.js'
import Nprogress from 'nprogress' //  导入进度条插件
import 'nprogress/nprogress.css'
const Login = () => import('@/views/login/index.vue')
const layout = () => import('@/views/layout/index.vue')
const Home = () => import('@/views/home/index.vue')
const Article = () => import('@/views/article/index.vue')
const Publish = () => import('@/views/publish/index.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/Login',
    name: 'Login',
    component: Login
  },
  // {
  //   path: '/home',
  //   alias: '/',
  //   component: home
  // }
  {
    path: '/',
    component: layout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: Home
      },
      {
        path: '/article',
        component: Article
      },
      {
        path: '/publish',
        component: Publish
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 前置守卫 还没进入to的路由
router.beforeEach((to, from, next) => {
  Nprogress.start()
  if (getItem()) {
    if (to.path.toLowerCase() === '/login') {
      next('/home')
    } else {
      next()
    }
  } else {
    if (to.path.toLowerCase() === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})

//  后置守卫  已进入to的路由
router.afterEach((to, from) => {
  Nprogress.done()

  // console.log('afterEach', to, from)
})
export default router
